/*
 * @Author: 高瑞寒 15932717091@163.com
 * @Date: 2022-05-10 23:52:00
 * @LastEditTime: 2022-05-16 10:29:11
 * @LastEditors: 高瑞寒 15932717091@163.com
 * @Description: 
 */
import axios from 'axios'
import { message } from 'antd'
import { getItem } from './storage';

// 配置项
const axiosOption = {
  baseURL: '/api',
  timeout: 5000
}

// 创建一个单例
const instance = axios.create(axiosOption);

// 添加请求拦截器
instance.interceptors.request.use(function (config) {
  const token = getItem('info')['cms-token'] || ''
  if (token) {
    config.headers['cms-token'] = token
  }
  return config;
},
  function (error) {
    // 对请求错误做些什么
    return Promise.reject(error);
  });

// 添加响应拦截器
instance.interceptors.response.use(function (response) {
  // 对响应数据做点什么
  const { errCode, message: msg } = response.data
  if (errCode !== 0) {
    message.error(msg)
    return Promise.reject(msg);
  }
  return response.data;
},
  function (error) {
    // 对响应错误做点什么
    return Promise.reject(error);
  });

export default instance;